package com.xm.xmapp.customview;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import com.xm.xmapp.R;

public class WaveView extends View {

	public static final int COLOR = 0xFFEC952F;
	// private float mMaxRadiusRate = 0.85f; // 如果没有设置mMaxRadius，可mMaxRadius =
	// 最小长度 * mMaxRadiusRate;
	private float mInitialRadiush; // 初始矩形高
	private float mInitialRadiusw; // 初始矩形宽
	private float mMaxRadiush; // 最大波纹半径
	private float mMaxRadiusw; // 最大波纹半径
	private long mDuration = 3000; // 一个波纹从创建到消失的持续时间
	private int mSpeed = 1000; // 波纹的创建速度，每500ms创建一个
	private Interpolator mInterpolator = new LinearInterpolator();

	private List<RectFactory> mCircleList = new ArrayList<RectFactory>();
	private boolean mIsRunning;

	// private boolean mMaxRadiusSet;

	private Paint mPaint, mPaint_text;
	private long mLastCreateTime;

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension((int) mMaxRadiusw, (int) mMaxRadiush);
	}

	private Runnable mCreateCircle = new Runnable() {
		@Override
		public void run() {
			if (mIsRunning) {
				newCircle();
				postDelayed(mCreateCircle, mSpeed);
			}
		}
	};

	public WaveView(Context context) {
		this(context, null);
	}

	float textw;
	float texth;
	
	float yuanjiao;

	public WaveView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint_text = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint_text.setColor(Color.BLACK);
		mPaint_text.setStyle(Paint.Style.FILL);
		mPaint_text.setTextSize(getResources().getDimension(R.dimen.smallsize));
		yuanjiao = getResources().getDimension(R.dimen.dp10); //圆角
		mInitialRadiush = getResources().getDimension(R.dimen.dp20); // 初始矩形高
		mInitialRadiusw = getResources().getDimension(R.dimen.dp90); // 初始矩形宽
		mMaxRadiush = mInitialRadiush
				+ getResources().getDimension(R.dimen.dp20); // 最大矩形高
		mMaxRadiusw = mInitialRadiusw
				+ getResources().getDimension(R.dimen.dp20); // 最小矩形宽

		texth = -(mPaint_text.ascent() + mPaint_text.descent());
		textw = mPaint_text.measureText("加入集团");

		setColor(COLOR);
		setStyle(Paint.Style.FILL);
	}

	public void setStyle(Paint.Style style) {
		mPaint.setStyle(style);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// if (!mMaxRadiusSet) {
		// mMaxRadiush =h * mMaxRadiusRate / 2.0f;
		// mMaxRadiusw =w * mMaxRadiusRate / 2.0f;
		// }
	}

	// public void setMaxRadiusRate(float maxRadiusRate) {
	// this.mMaxRadiusRate = maxRadiusRate;
	// }

	public void setColor(int color) {
		mPaint.setColor(color);
	}

	/**
	 * 开始
	 */
	public void start() {
		if (!mIsRunning) {
			mIsRunning = true;
			mCreateCircle.run();
		}
	}

	/**
	 * 停止
	 */
	public void stop() {
		mIsRunning = false;
	}

	protected void onDraw(Canvas canvas) {
		Iterator<RectFactory> iterator = mCircleList.iterator();

		while (iterator.hasNext()) {
			RectFactory circle = iterator.next();
			if (System.currentTimeMillis() - circle.mCreateTime < mDuration) {
				
				mPaint.setAlpha(circle.getAlpha());
				Rect bj = circle.getCurrentRadius();
				float x1 = getWidth() / 2 - bj.width / 2;
				float y1 = getHeight() / 2 - bj.height / 2;
				float x2 = getWidth() / 2 + bj.width / 2;
				float y2 = getHeight() / 2 + bj.height / 2;

				RectF oval3 = new RectF(x1, y1, x2, y2);// 设置个新的长方形

				canvas.drawRoundRect(oval3, yuanjiao, yuanjiao, mPaint);// 第二个参数是x半径，第三个参数是y半径

			} else {
				iterator.remove();
			}
		}

		// private float mInitialRadiush = 50; // 初始波纹半径
		// private float mInitialRadiusw = 200; // 初始波纹半径
		setColor(COLOR);
		RectF oval3 = new RectF(getWidth() / 2 - mInitialRadiusw / 2,
				getHeight() / 2 - mInitialRadiush / 2, getWidth() / 2
						+ mInitialRadiusw / 2, getHeight() / 2
						+ mInitialRadiush / 2);// 设置个新的长方形

		canvas.drawRoundRect(oval3, yuanjiao, yuanjiao, mPaint);// 第二个参数是x半径，第三个参数是y半径

		canvas.drawText("加入集团", getWidth() / 2f - textw / 2f, getHeight() / 2f
				+ texth / 2f, mPaint_text);

		if (mCircleList.size() > 0) {
			postInvalidateDelayed(10);
		}
	}

	public void setDuration(long duration) {
		this.mDuration = duration;
	}

	// public void setMaxRadius(float maxRadius) {
	// this.mMaxRadius = maxRadius;
	// mMaxRadiusSet = true;
	// }

	public void setSpeed(int speed) {
		mSpeed = speed;
	}

	private void newCircle() {
		long currentTime = System.currentTimeMillis();
		if (currentTime - mLastCreateTime < mSpeed) {
			return;
		}
		RectFactory rect = new RectFactory();
		mCircleList.add(rect);
		invalidate();
		mLastCreateTime = currentTime;
	}

	private class RectFactory {

		private long mCreateTime;

		public RectFactory() {
			this.mCreateTime = System.currentTimeMillis();
		}

		public int getAlpha() {
			float percent = (System.currentTimeMillis() - mCreateTime) * 1.0f
					/ mDuration;
			float colorAlpha = mInterpolator.getInterpolation(percent);

			return (int) ((1.0f - colorAlpha) * 255);
		}

		public Rect getCurrentRadius() {
			float percent = (System.currentTimeMillis() - mCreateTime) * 1.0f
					/ mDuration;

			float currentRadius = mInterpolator.getInterpolation(percent);

			float width = mInitialRadiusw
					+ currentRadius
					* (mMaxRadiusw - mInitialRadiusw);
			float height = mInitialRadiush
					+ currentRadius
					* (mMaxRadiush - mInitialRadiush);

			return new Rect(width, height);
		}
	}

	private class Rect {
		private float width;
		private float height;

		public Rect(float width, float height) {
			this.width = width;
			this.height = height;
		}
	}

	public void setInterpolator(Interpolator interpolator) {
		mInterpolator = interpolator;
		if (mInterpolator == null) {
			mInterpolator = new LinearInterpolator();
		}
	}
}
